<!DOCTYPE html>
<meta charset="utf-8">
<title>RTCEncodedAudioFrame can be cloned and distributed</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src=/resources/testdriver.js></script>
<script src=/resources/testdriver-vendor.js></script>
<script src='../../mediacapture-streams/permission-helper.js'></script>
<script src="../../webrtc/RTCPeerConnection-helper.js"></script>
<script src="../../service-workers/service-worker/resources/test-helpers.sub.js"></script>

<script>
"use strict";
promise_test(async t => {
  const caller1 = new RTCPeerConnection();
  t.add_cleanup(() => caller1.close());
  const callee1 = new RTCPeerConnection({encodedInsertableStreams:true});
  t.add_cleanup(() => callee1.close());
  await setMediaPermission("granted", ["microphone"]);
  const inputStream = await navigator.mediaDevices.getUserMedia({audio:true});
  const inputTrack = inputStream.getAudioTracks()[0];
  t.add_cleanup(() => inputTrack.stop());
  caller1.addTrack(inputTrack)
  exchangeIceCandidates(caller1, callee1);

  const caller2 = new RTCPeerConnection({encodedInsertableStreams:true});
  t.add_cleanup(() => caller2.close());
  const callee2 = new RTCPeerConnection({encodedInsertableStreams:true});
  t.add_cleanup(() => callee2.close());
  const sender2 = caller2.addTransceiver("audio").sender;
  const writer2 = sender2.createEncodedStreams().writable.getWriter();
  sender2.replaceTrack(new MediaStreamTrackGenerator({ kind: 'audio' }));
  exchangeIceCandidates(caller2, callee2);

  const IncomingframesSentAndReceivedCorrectly = new Promise((resolve, reject) => {
    // Write the received incoming frames on callee1 to caller2.
    callee1.ontrack = async e => {
      const receiverStreams = e.receiver.createEncodedStreams();
      const receiverReader = receiverStreams.readable.getReader();
      const result = await receiverReader.read();
      const original = result.value;
      await writer2.write(original);
      resolve();
    }

        // callee2 receives frames over the PC from caller2.
    callee2.ontrack = async e => {
      const receiverStreams = e.receiver.createEncodedStreams();
      const receiverReader = receiverStreams.readable.getReader();
      const receiverWriter = receiverStreams.writable.getWriter();
      const result = await receiverReader.read();
      receiverWriter.write(result.value);
      resolve();
    }

  });

  await exchangeOfferAnswer(caller1, callee1);
  await exchangeOfferAnswer(caller2, callee2);

  return IncomingframesSentAndReceivedCorrectly;
}, "Send endoded incoming frame");
</script>
